package com.wuxianedu.urldemo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by TED on 2016/9/26.
 */
public class Utils {

    public static void myMethod() throws IOException {
        getMethod();
    }

    public static void getMethod() throws IOException {
        //http://h.hiphotos.baidu.com/zhidao/pic/item/bf096b63f6246b607320425deff81a4c510fa2e9.jpg
        URL url = new URL("http://www.51cto.com/");
        Log.e("------url-------", url.getProtocol());// 获取协议
        Log.e("------url-------", url.getHost());// 获取主机
        Log.e("------url-------", url.getPort() + "");// 获取端口号
        Log.e("------url-------", url.getPath());// 获取路径
        Log.e("------url-------", url.getFile());// 获取资源

        URLConnection conn = url.openConnection();

        // 这是请求头
        conn.setRequestProperty("Connection", "Keep-Alive"); // 设置长连接
        conn.setRequestProperty("Charset", "utf-8"); // 设置编码集
        conn.setConnectTimeout(10000); // 设置请求超时 10s
        // GET请求
        conn.connect();
        // 请求的内容在流里
        InputStream is = conn.getInputStream();
        // Bitmap bitmap = BitmapFactory.decodeStream(is);

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String str;
        while ((str = br.readLine()) != null) {
            Log.e("----", str);
        }
    }


    public static Bitmap openStream() throws IOException {
        URL url = new URL("http://h.hiphotos.baidu.com/zhidao/pic/item/bf096b63f6246b607320425deff81a4c510fa2e9.jpg");
        URLConnection conn = url.openConnection();
        conn.setConnectTimeout(10000); // 设置请求超时 10s
        // GET请求
        conn.connect();
        // 请求的内容在流里
        InputStream is = conn.getInputStream();
        Bitmap bitmap = BitmapFactory.decodeStream(is);
        return bitmap;
    }
}
